package com.summer.mall.controller.admin;

import com.google.common.collect.Maps;
import com.summer.mall.common.Const;
import com.summer.mall.common.ResponseCode;
import com.summer.mall.common.ResponseService;
import com.summer.mall.entity.Product;
import com.summer.mall.entity.User;
import com.summer.mall.service.ProductService;
import com.summer.mall.service.UserService;
import com.summer.mall.util.FileUploadUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/manage/product")
public class ProductManageController {

    @Autowired
    private ProductService productService;

    @Autowired
    private UserService userService;

    @Autowired
    private HttpSession httpSession;



    @RequestMapping("/save")
    @ResponseBody
    public ResponseService saveOrUpdateProduct(HttpSession session, Product product){
        User currentUser = (User)session.getAttribute(Const.CURRENT_USER);
        if (currentUser == null){
            return ResponseService.createByCodeErrorMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        //校验是不是管理员用户
        if (userService.checkAdminRole(currentUser).isSuccess()){
            return productService.saveOrUpadteProduct(product);
        }else{
            return userService.checkAdminRole(currentUser);
        }

    }

    @RequestMapping("/set_sale_status")
    @ResponseBody
    public ResponseService setSaleStatus(HttpSession session, Integer productId,Integer status){
        User currentUser = (User)session.getAttribute(Const.CURRENT_USER);
        if (currentUser == null){
            return ResponseService.createByCodeErrorMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        //校验是不是管理员用户
        if (userService.checkAdminRole(currentUser).isSuccess()){
            return productService.setSaleStatus(productId,status);
        }else{
            return userService.checkAdminRole(currentUser);
        }
    }


    //获取产品详情的接口
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseService getDetail(HttpSession session, Integer productId){
        User currentUser = (User)session.getAttribute(Const.CURRENT_USER);
        if (currentUser == null){
            return ResponseService.createByCodeErrorMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        //校验是不是管理员用户
        if (userService.checkAdminRole(currentUser).isSuccess()){
            return productService.manageProductDetail(productId);
        }else{
            return userService.checkAdminRole(currentUser);
        }
    }

    //后台产品分页接口
    @RequestMapping("/list")
    @ResponseBody
    public ResponseService getDetail(HttpSession session, @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum , @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize){
        User currentUser = (User)session.getAttribute(Const.CURRENT_USER);
        if (currentUser == null){
            return ResponseService.createByCodeErrorMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        //校验是不是管理员用户
        if (userService.checkAdminRole(currentUser).isSuccess()){
            return productService.getProductList(pageNum, pageSize);
        }else{
            return userService.checkAdminRole(currentUser);
        }
    }

    //后台产品产品搜索接口
    @RequestMapping("/search")
    @ResponseBody
    public ResponseService searchProduct(HttpSession session, @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum ,@RequestParam(value ="pageSize",defaultValue = "10")Integer pageSize, Integer productId ,String productName){
        User currentUser = (User)session.getAttribute(Const.CURRENT_USER);
        if (currentUser == null){
            return ResponseService.createByCodeErrorMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        //校验是不是管理员用户
        if (userService.checkAdminRole(currentUser).isSuccess()){
            return productService.searchProductByProductNameAndId(pageNum,pageSize,productId,productName);
        }else{
            return userService.checkAdminRole(currentUser);
        }
    }

    @RequestMapping("/upload")
    @ResponseBody
    public ResponseService upload(@RequestParam(value = "upload_file",required = false) MultipartFile file){
        Map<String,String> fileMap = new HashMap<String,String>() ;
        User currentUser = (User)httpSession.getAttribute(Const.CURRENT_USER);
        if (currentUser == null){
            return ResponseService.createByCodeErrorMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        //校验是不是管理员用户
        if (userService.checkAdminRole(currentUser).isSuccess()){
            String targetFileName = FileUploadUtil.saveProductImage(file);
            fileMap.put("upload_product_image_url",targetFileName);
            return ResponseService.createBySuccess();
        }else{
            return  userService.checkAdminRole(currentUser);
        }
    }

    //编写商品介绍时，选用富文本编辑器编写产品信息
    //simditor富文本上传对返回只有自己的要求  富文本中对于返回值有自己的要求,我们使用是simditor所以按照simditor的要求进行返回
    //      { "success": true/false,
    //       "msg": "error message", # optional
    //         "file_path": "[real file path]"
    //        }

    @RequestMapping("/richtext_img_upload")
    @ResponseBody
    public Map richTextImgUpload(@RequestParam(value = "upload_file",required = false) MultipartFile file, HttpServletResponse response) {

        Map resultMap = Maps.newHashMap();
        User currentUser = (User)httpSession.getAttribute(Const.CURRENT_USER);
        if (currentUser == null){
            resultMap.put("success",false);
            resultMap.put("msg","未登录");
            return resultMap;
        }

        if (userService.checkAdminRole(currentUser).isSuccess()){

            String targetFileName = FileUploadUtil.saveProductImage(file);
            if (StringUtils.isBlank(targetFileName)){
                resultMap.put("success",false);
                resultMap.put("msg","上传失败");
                return resultMap;
            }

            resultMap.put("success",true);
            resultMap.put("msg","上传成功");
            resultMap.put("file_path",targetFileName);
            //修改一下responsede header   simditor  文档规范
            response.addHeader("Access_Control-Allow-Headers","X-File-Name");
            return resultMap;
        }else{
            resultMap.put("success",false);
            resultMap.put("msg","无权限操作");
            return resultMap;
        }

    }

}
